package com.whz.builder;

import java.util.EnumSet;
import java.util.Objects;
import java.util.Set;

/**
 * @Author: sinosoft
 * @Description: Builder模式适用于类层次结构
 * @Date: Create in 20:31 2021/4/27
 * @Modified By:
 */
public class Pizza {

    /*配料*/
    public enum Topping {HAM, MUSHROOM, ONION, PEPPER, SAUSAGE}
    final Set<Topping> toppings;

    abstract static class Builder<T extends Builder<T>> {
        /*通过静态工厂方法得到EnumSet对象*/
        EnumSet<Topping> toppings = EnumSet.noneOf(Topping.class);
        public T addTopping(Topping topping) {
            toppings.add(Objects.requireNonNull(topping));
            return self();
        }
        abstract Pizza build();
        /*子类必须重写这个方法 以返回this*/
        protected abstract T self();
    }
    Pizza(Builder<?> builder) {
        toppings = builder.toppings.clone();
    }
}
